package com.weiaibenpao.demo.chislim.ui.activity.discover;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.weiaibenpao.demo.chislim.R;
import com.weiaibenpao.demo.chislim.base.BaseActivity;
import com.weiaibenpao.demo.chislim.bean.TravelBean;
import com.weiaibenpao.demo.chislim.presenter.DjqTravelPresenter;
import com.weiaibenpao.demo.chislim.ui.adapter.DjqTravelAdapter;
import com.weiaibenpao.demo.chislim.utils.Constant;
import com.weiaibenpao.demo.chislim.view.DjqTravelView;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class TourismActivity extends BaseActivity<DjqTravelView, DjqTravelPresenter> implements DjqTravelView ,
        BaseQuickAdapter.RequestLoadMoreListener,SwipeRefreshLayout.OnRefreshListener,BaseQuickAdapter.OnItemClickListener{
    public static int START_NUM = 1;

    @BindView(R.id.myMatch)
    RecyclerView myTourism;
    @BindView(R.id.mid_text)
    TextView midText;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    DjqTravelAdapter djqTravelAdapter;
    int page = START_NUM;
    private View view;


    /**
     * 发现 - 旅游推荐
     * author lijianbao
     */
    @Override
    public int setLayoutResourceID() {
        return R.layout.activity_match;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        midText.setText("旅游推荐");
        initAdapter();
        swipeLayout.setRefreshing(true);
        swipeLayout.setOnRefreshListener(this);
        doRefresh();
//        djqTravelAdapter = new DjqTravelAdapter(this);
//        myTourism.setLayoutManager(new LinearLayoutManager(this,
//                LinearLayoutManager.VERTICAL, false));
//        myTourism.setAdapter(djqTravelAdapter);
//
//        mPresenter.getDjqTravel(page, 50);
//
//
//        DjqTravelAdapter.setOnItemClickListener(new DjqTravelAdapter.OnItemClickListener() {
//            @Override
//            public void setOnItemClick(View view, String h5_url) {
//                Logger.e("ljb:" + h5_url);
//                Intent intent = new Intent(TourismActivity.this, WebViewActivity.class);
//                intent.putExtra("h5_url", h5_url);
//                intent.putExtra("h5_title", "旅游推荐");
//                startActivity(intent);
//            }
//        });
    }

    private void initAdapter() {
        djqTravelAdapter = new DjqTravelAdapter();
        djqTravelAdapter.setOnItemClickListener(this);
        myTourism.setLayoutManager(new LinearLayoutManager(this));
        myTourism.setAdapter(djqTravelAdapter);
         view = LayoutInflater.from(this).inflate(R.layout.layout_empty, null);
        swipeLayout.setOnRefreshListener(this);
        swipeLayout.setRefreshing(true);

    }

    @Override
    public void djqTravelSuccess(TravelBean travelBean) {
        swipeLayout.setRefreshing(false);
        djqTravelAdapter.setEmptyView(view);

        boolean isRefresh=page==1;
        setData(travelBean.getList(),isRefresh);
    }

    @Override
    public void djqTravelFail(String msg) {

    }
    private void doRequest() {

        mPresenter.getDjqTravel( page, Constant.DEFAULT_PAGE_SIZE);
    }
    private void setData(List<TravelBean.ListBean> marathoList, boolean isRefresh) {
        int size = marathoList != null ? marathoList.size() : 0;
        if (page == 1) {
            djqTravelAdapter.setNewData(marathoList);

        } else {
            djqTravelAdapter.addData(marathoList);
            djqTravelAdapter.loadMoreComplete();
        }
        if (size < Constant.DEFAULT_PAGE_SIZE) {
            //第一页如果不够一页就不显示没有更多数据布局
            djqTravelAdapter.loadMoreEnd();
        } else {
            djqTravelAdapter.loadMoreComplete();
        }
    }

    @Override
    public DjqTravelPresenter initPresenter() {
        return new DjqTravelPresenter();
    }



    @OnClick(R.id.left_image)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void onLoadMoreRequested() {
        page++;
        doRequest();
    }

    @Override
    public void onRefresh() {
        doRefresh();

    }
    private void doRefresh() {
        page=1;
        djqTravelAdapter.setEnableLoadMore(false);
        doRequest();
    }


//    @Override
//    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
//        WebViewActivity.startSelf(this,djqTravelAdapter.getItem(position).getTravelContentUrl(),"旅游攻略",1);
////        Intent intent = new Intent(TourismActivity.this, WebViewActivity.class);
////        intent.putExtra("h5_url",djqTravelAdapter.getItem(position).getTravelContentUrl());
////        intent.putExtra("h5_title", "旅游攻略");
////        startActivity(intent);
//    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        WebViewActivity.startSelf(this,djqTravelAdapter.getItem(position).getTravelContentUrl(),"旅游攻略",1);

    }
}
